package jobs;

import facade.base.CorpFacade;
import helper.TextHelper;
import jodd.util.RandomString;
import models.base.AdminUser;
import models.base.Corp;
import models.base.UserHistory;
import models.constants.DeletedStatus;
import models.enums.AvailableStatus;
import models.enums.LicenseEdition;
import org.slf4j.LoggerFactory;
import play.Logger;
import play.Play;
import play.jobs.Job;
import play.jobs.OnApplicationStart;
import util.DateHelper;

import java.util.Date;

/**
 * 启动时自动建立测试用数据库配置.
 */
@OnApplicationStart
public class BootstrapData extends Job {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(BootstrapData.class);

    public void doJob() {
        //System.getProperties().setProperty("oracle.jdbc.J2EE13Compliant", "true");
        //Logger.info("LOG00200: start Application mode is %s ---", Play.mode.isDev());


        //如果超级管理员不存在，则创建一个
         if(findSuperAdmin() == null)
             createAdministrator();

//        if (CorpFacade.isPrivateInstallVersion()) {
//            List<Corp> corps = createCorp();
//            for (Corp corp : corps) {
//                createDefaultAdminUser(corp);
//                List<OfficeArea> officeAreas = OfficeArea.findAllByCorp(corp);
//                if (officeAreas.size() == 0) {
//                    // 生成一个默认OfficeArea
//                    OfficeArea.create(corp, Messages.get("office-areas.default-name"));
//                }
//            }
//
//        }

        //if (Play.mode.isDev() && !Play.runingInTestMode()) {  //开发模式下加载测试数据
//        if (1 == 1) {
//            Logger.info("加载测试数据");
//            if (AdminUser.count() < 3) {
//                // 生成测试用公司
//                createMainCorp();
//            }
//        } else {
//            Logger.info("没有要加载的数据...");
//        }
    }



    /**
     * 生成主要测试用公司.
     */
    private void createMainCorp() {
        String name = "地精银行";

        Corp corp = Corp.findAvailableByName(name);
        if (corp != null) {
            Logger.info("LOG83613: 已经存在" + name + "对应的公司");
            return;
        }

        // 生成公司.
        corp = CorpFacade.createWithApp(name, 360);
        corp.deleted=models.constants.DeletedStatus.UN_DELETED;
        corp.expiredAt = DateHelper.afterDays(360);
        corp.licenseEdition = LicenseEdition.TESTING;
        corp.save();

//        Dept dept1 = createDept(corp, "安防部");
//        Dept dept2 = createDept(corp, "市场部");
//
//        OfficeArea officeArea = OfficeArea.create(corp, Messages.get("office-areas.default-name"));

//        AdminUser admin = createUser(corp, dept2, "管理员", "admin", "15026682161");
        AdminUser admin = createUser(corp, "管理员", "admin", "15026682161");
//        admin.country = "中国";
//        admin.city = "闵行";
//        admin.confirmedAt = new Date();
        admin.email = "admin@safeguard.com";
        //tangliqun.headImgUrl = "http://wx.qlogo.cn/mmopen/PiajxSqBRaEK7a8M92hLk8TSwuvwMgeY06f5PmrYz6jpgv3JyhuiaCyiboHASPZ3icAGsiaowQCva928f33SOn47tNA/0";
//        admin.isAdmin = Boolean.TRUE;
//        admin.nickName = "管理员";
//        admin.sex = Gender.MALE;
        admin.updatePassword("abc123");
        //UserOpenId.create("oz8A1uPlFb-SZoHNbI9ZaPY9ljbU", tangliqun, OpenIdSource.WXMP);
        //tangliqun.weixinUnionId = "oKSOjt4xgqpbRIQOXkLeO-TNve6Q";
        admin.save();

        AdminUser user1 = createUser(corp, "张三丰", "zhangsf", "18512101252");
        user1.email = "15026682165@163.com";
        user1.updatePassword("abc456");
        user1.save();
        AdminUser user2 = createUser(corp, "洪七公", "hongqg", "13121011002");
        user2.email = "leo@safeguard.com";
        user2.save();
//        AdminUser user3 = createUser(corp, dept1, "欧阳峰", "ouyangf", "13121022003");
//        user3.email = "ouyf@hyshitest.cn";
//        user3.save();
//        AdminUser user4 = createUser(corp, dept2, "殷天正", "ytz", "13121033025");
//        AdminUser user5 = createUser(corp, dept2, "黄蓉", "huangrong", "13821033027");
//        //user5.weixinUnionId = "oKSOjtxQT_vgev5Tl94Ua-5DmeZU";
//        //user5.isAdmin = Boolean.TRUE;
//        user5.save();
//        AdminUser user6 = createUser(corp, dept2, "杨过", "yangguo", "13821033097");
//        AdminUser user7 = createUser(corp, dept2, "段誉", "duany", "13929331097");
//        createUser(corp, dept2, "马铨", "maquan", "13929331197");
//        createUser(corp, dept2, "乔峰", "qiaofeng", "13929331198");
//        createUser(corp, dept2, "张无忌", "zhangwj", "13911331198");
//        createUser(corp, dept2, "韦小宝", "weixb", "13911332298");
//        createUser(corp, dept2, "袁承志", "yuancz", "13911333398");
//        createUser(corp, dept2, "令狐冲", "linghc", "13925333398");
//        createUser(corp, dept2, "东方不败", "dfbb", "13925333399");
//        createUser(corp, dept2, "慕容复", "mulf", "13925335599");
//        createUser(corp, dept2, "扫地僧", "shaods", "13921135599");
//        createUser(corp, dept2, "觉远大师", "jueyuan", "13821135599");
//        createUser(corp, dept2, "何足道", "hezdao", "13821133799");
//        createUser(corp, dept2, "胡斐", "huhei", "13721133799");
//        createUser(corp, dept2, "石破天", "shipt", "13721133010");
//        createUser(corp, dept2, "向问天", "xiangwt", "13721133011");
//        createUser(corp, dept2, "任我行", "renwx", "13721133012");
        AdminUser tangliqun = createUser(corp, "郭靖", "tangliqun", "15026682161");
//        AdminUser tangliqun = createUser(corp, dept2, "郭靖", "tangliqun", "15026682161");
//        tangliqun.country = "中国";
//        tangliqun.city = "闵行";
//        tangliqun.confirmedAt = new Date();
        tangliqun.email = "yang.shancheng@qq.com";
        //tangliqun.headImgUrl = "http://wx.qlogo.cn/mmopen/PiajxSqBRaEK7a8M92hLk8TSwuvwMgeY06f5PmrYz6jpgv3JyhuiaCyiboHASPZ3icAGsiaowQCva928f33SOn47tNA/0";
//        tangliqun.isAdmin = Boolean.TRUE;
//        tangliqun.nickName = "郭靖";
//        tangliqun.sex = Gender.MALE;
//        tangliqun.updatePassword("abc123");
        //UserOpenId.create("oz8A1uPlFb-SZoHNbI9ZaPY9ljbU", tangliqun, OpenIdSource.WXMP);
        //tangliqun.weixinUnionId = "oKSOjt4xgqpbRIQOXkLeO-TNve6Q";
        tangliqun.save();

        Logger.info("加载测试公司成功");
    }

    /**
     * 生成初始用户信息.
     */
    private static AdminUser buildNewUser() {
        AdminUser user = new AdminUser();
        user.createdAt = new Date();
        user.modifyAt = new Date();
        user.status = AvailableStatus.AVAILABLE;
        return user;
    }
//    private AdminUser createUser(Corp corp, Dept dept, String name, String userId, String mobile) {
    private AdminUser createUser(Corp corp, String name, String userId, String mobile) {
        AdminUser user = buildNewUser();
        user.corp = corp;
        user.email = userId + "@mycorp.com";
        user.name = name;
        user.mobile = mobile;
        user.loginName = userId;
        user.save();
        UserHistory.snapUser(user, null, "Boot新建");
        return user;
    }


    //生成超级管理员
    private void  createAdministrator(){
        AdminUser administrator = new AdminUser();
        administrator.isSuperAdmin = Boolean.TRUE;
        administrator.name = "administrator";
        administrator.loginName = "administrator";
        administrator.mobile = "18006330633";
        administrator.email = "migao@migao.com";
        //初始密码
        administrator.passwordSalt = RandomString.getInstance().randomAlphaNumeric(30);
        administrator.password = TextHelper.encryptPassword("administrator", administrator.passwordSalt);
        administrator.createdAt = new Date();
        administrator.save();

    }

    private AdminUser findSuperAdmin(){
        return AdminUser.find("deleted = ? and isSuperAdmin = ? " , DeletedStatus.UN_DELETED , Boolean.TRUE).first();
    }


}
